package com.sure0000.basic;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @program: demo
 * @description: cron 调度
 *
 * 调度常用的两种方式：SimpleScheduleBuilder、CronScheduleBuilder
 * @author: xuyouchang@supcon.com
 * @create: 2019-12-26 10:45
 **/
public class QuartzCron {

    // 创建调度器
    private static Scheduler getScheduler() throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        return schedulerFactory.getScheduler();
    }

    public static void schedulerJob() throws SchedulerException{
        //创建任务
        JobDetail jobDetail = JobBuilder.newJob(QuartzCronJob.class).build();
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
                .build();

        Scheduler scheduler = getScheduler();
        //将任务及其触发器放入调度器
        scheduler.scheduleJob(jobDetail, trigger);
        //调度器开始调度任务
        scheduler.start();

    }

    public static void main(String[] args) throws SchedulerException {
        QuartzCron mainScheduler = new QuartzCron();
        mainScheduler.schedulerJob();
    }
}
